# -*- coding: utf-8 -*-
from selenium import webdriver
import os,time
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

file_path = os.path.abspath('table_test.htm')
driver = webdriver.Chrome()
driver.get(file_path)
table = driver.find_element_by_id('tab1')

#table的总行数，包含标题
table_rows = table.find_elements_by_tag_name('tr')
print("总行数:",len(table_rows))

#tabler的总列数
'''
在table中找到第一个tr,之后在其下找到所有的th,即是tabler的总列数
'''
table_cols = table_rows[0].find_elements_by_tag_name('th')
print("总列数:",len(table_cols))

#获取某单元格的text:获取第一行第二列的text,[不算标题行]
row1_col2 = table_rows[1].find_elements_by_tag_name('td')[1].text
print ("第一行第二列的text:",row1_col2)

#删除最后一行
table_rows[-1].find_element_by_tag_name('input').click()
time.sleep(2)
driver.switch_to_alert().accept()
time.sleep(2)